package cn.tqfeiyang.springplus.generator.jdbc;

import cn.tqfeiyang.springplus.generator.config.GeneratorConfig;
import cn.tqfeiyang.springplus.generator.name.NameConverter;
import cn.tqfeiyang.springplus.generator.name.NameFormat;
import lombok.Data;

/**
 * 字段验证信息
 *
 * @author tqfeiyang
 * @since 2024/10/8 15:55
 */
@Data
public class ValidatorInfo {
    private GeneratorConfig generatorConfig;
    private FieldInfo fieldInfo;
    private String validatorMethod;
    private String commentFormat;
    private String messageFormat;
    private String importClassName;

    private String annotationName; //注解名称

    private boolean includeKeyParameter = false; // 是否包含id参数

    public ValidatorInfo(GeneratorConfig generatorConfig, FieldInfo fieldInfo, String annotationName, String validatorMethod, String commentFormat, String messageFormat, String importClassName) {
        this.generatorConfig = generatorConfig;
        this.fieldInfo = fieldInfo;
        this.annotationName = annotationName;
        this.validatorMethod = validatorMethod;
        this.commentFormat = commentFormat;
        this.messageFormat = messageFormat;
        this.importClassName = importClassName;
    }

    public ValidatorInfo(GeneratorConfig generatorConfig, FieldInfo fieldInfo, String annotationName, String validatorMethod, String commentFormat, String messageFormat, String importClassName, boolean includeKeyParameter) {
        this.generatorConfig = generatorConfig;
        this.fieldInfo = fieldInfo;
        this.annotationName = annotationName;
        this.validatorMethod = validatorMethod;
        this.commentFormat = commentFormat;
        this.messageFormat = messageFormat;
        this.importClassName = importClassName;
        this.includeKeyParameter = includeKeyParameter;
    }

    public String getCommentText() {
        return String.format(commentFormat, fieldInfo.getComment());
    }

    public String getMessageText() {
        return String.format(messageFormat, fieldInfo.getComment());
    }

    public String getParameterText() {
        NameConverter nameConverter = generatorConfig.getNameConverter();
        String parameterText = String.format("entity.%s()", nameConverter.convert(fieldInfo, NameFormat.GetterMethod));
        if (includeKeyParameter) {
            parameterText += String.format(", entity.%s()", nameConverter.convert(fieldInfo.getTable().getKeyField(), NameFormat.GetterMethod));
        }
        return parameterText;
    }
}
